﻿using Entity;
using IRealize;
using Microsoft.AspNetCore.Mvc;
using Realize;
using System.Runtime.InteropServices.JavaScript;

namespace WebApplication9.Controllers.Permissions_Manage
{
    public class User_ManageController : Controller
    {
        private readonly IConfiguration configuration;
        public User_ManageController(IConfiguration configuration)
        {
            this.configuration = configuration;
        }
        public IActionResult Index()
        {
            return View();
        }

        public async Task<IActionResult> Select(int CurrentPage,int PageSize)
        {
            IUR uR = new UR();
            List<UserRolesSelect> zhi = await uR.Select_UR(configuration["SqlServer"]);
            List<UserRolesSelect> zhi2 = zhi.Skip((CurrentPage - 1) * PageSize).Take(PageSize).ToList();
            PagedViewModel<UserRolesSelect> cc = new PagedViewModel<UserRolesSelect>() { itemas = zhi2, total = zhi.Count };
            return Ok(new
            {
                list = cc
            });
        }

        public IActionResult Add()
        {
            return View();
        }

        public async Task<IActionResult> select_r()
        {
            IR iR = new R();
            List<Roles> roles = await iR.select(configuration["SqlServer"]);
            return Ok(new
            {
                zhi = roles
            });
        }

        public async Task<IActionResult> AddUR(string name,string zsname,string pwd,string roles)
        {
            IUR uR = new UR();
            int result = await uR.Add_UR(name,zsname,pwd,roles,configuration["SqlServer"]);
            if (result > 0)
            {
                return Ok(new
                {
                    zhi = "ok"
                });
            }
            else
            {
                return Ok(new
                {
                    zhi = "nook"
                });
            }
        }

        public async Task<IActionResult> Delete(string id,string uid)
        {
            IUR uR = new UR();
            int result = await uR.Delect_UR(id,uid, configuration["SqlServer"]);
            if (result > 0)
            {
                return Ok(new
                {
                    zhi = "ok"
                });
            }
            else
            {
                return Ok(new
                {
                    zhi="no"
                });
            }
        }

        public IActionResult Upd(string id)
        {
            ViewBag.TitleId = id;
            return View();
        }

        public async Task<IActionResult> selectById(string id)
        {
            IUR uR = new UR();
            UserRolesSelect userRolesSelect = await uR.SelectById(id, configuration["SqlServer"]);
            return Ok(new
            {
                zhi = userRolesSelect
            });
        }

        public async Task<IActionResult> UpdUR(string urid, string id, string name, string pwd, string zsname, string roles)
        {
            IUR uR = new UR();
            int reslut = await uR.Update_UR(urid,id,name,pwd,zsname,roles, configuration["SqlServer"]);
            if(reslut > 0)
            {
                return Ok(new
                {
                    zhi = "ok"
                });
            }
            else
            {
                return Ok(new
                {
                    zhi="no"
                });
            }
        }
    }
}
